﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
using System.Data.Entity;
namespace BenefitHelper.Data.Week
{
    /// <summary>
    /// 小组周累计基本数据
    /// </summary>
    public class GroupWeekSum
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int GroupId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Groups Group { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }
        /// <summary>
        /// 交易周
        /// </summary>
        public int Week { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }
        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }




        /// <summary>
        /// 删除当月所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int week, int month, int year, DB.DBManager db)
        {
            var query = from t in db.GroupWeekSum where t.Week == week && t.Year == year select t;
            foreach (Data.Week.GroupWeekSum c in query)
            {
                db.GroupWeekSum.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存周小组累计值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitGroupWeekSum(int week, int month, int year, DB.DBManager db)
        {
            List<Sys.Groups> groups = db.Groups.ToList();
            foreach (Sys.Groups g in groups)
            {
                GroupWeekSum day = new GroupWeekSum();
                day.GroupId = g.Id;
                day.Month = month;
                day.Year = year;
                day.Week = week;
                var queryExist = from t in db.GroupWeekSum where t.Week == week && t.Year == year && t.GroupId == g.Id select t;

                var query = from t in db.GroupDayChange where t.TradeHistory.WPdate == week && t.TradeHistory.YPdate == year && t.GroupId == g.Id select t;
                if (queryExist.Count() > 0)
                {
                    if (query.Count() > 0)
                    {
                        queryExist.First().BillCount = query.Sum(a => a.BillCount);
                        queryExist.First().DayCount = query.Sum(a => a.DayCount);
                        queryExist.First().Free = query.Sum(a => a.Free);
                        queryExist.First().LoseCount = Convert.ToInt32(query.Sum(a => a.LoseCount));
                        queryExist.First().Profit = query.Sum(a => a.Profit);
                        queryExist.First().SumLost = query.Sum(a => a.SumLost);
                        queryExist.First().SumWin = query.Sum(a => a.SumWin);
                        queryExist.First().UsedMargin = query.Sum(a => a.UsedMargin);
                        queryExist.First().WinCount = Convert.ToInt32(query.Sum(a => a.WinCount));
                        queryExist.First().AvgBillBenefit = query.Sum(a => a.AvgBillBenefit);
                        queryExist.First().AvgPostionTime = query.Sum(a => a.AvgPostionTime);
                        queryExist.First().MaxReturn = query.Sum(a => a.MaxReturn);
                        queryExist.First().OverStopLoss = query.Sum(a => a.OverStopLoss);
                        queryExist.First().PingJinPing = query.Sum(a => a.PingJinPing);
                        queryExist.First().RaiseRate = query.Sum(a => a.RaiseRate);
                        queryExist.First().RiskCount = query.Sum(a => a.RiskCount);
                        queryExist.First().Utilization = query.Sum(a => a.Utilization);
                        queryExist.First().WinRate = query.Sum(a => a.WinRate);
                        queryExist.First().Yield = query.Sum(a => a.Yield);
                    }
                }
                else
                {
                    if (query.Count() > 0)
                    {
                        day.BillCount = query.Sum(a => a.BillCount);
                        day.DayCount = query.Sum(a => a.DayCount);
                        day.Free = query.Sum(a => a.Free);
                        day.LoseCount = Convert.ToInt32(query.Sum(a => a.LoseCount));
                        day.Profit = query.Sum(a => a.Profit);
                        day.SumLost = query.Sum(a => a.SumLost);
                        day.SumWin = query.Sum(a => a.SumWin);
                        day.UsedMargin = query.Sum(a => a.UsedMargin);
                        day.WinCount = Convert.ToInt32(query.Sum(a => a.WinCount));
                        day.AvgBillBenefit = query.Sum(a => a.AvgBillBenefit);
                        day.AvgPostionTime = query.Sum(a => a.AvgPostionTime);
                        day.MaxReturn = query.Sum(a => a.MaxReturn);
                        day.OverStopLoss = query.Sum(a => a.OverStopLoss);
                        day.PingJinPing = query.Sum(a => a.PingJinPing);
                        day.RaiseRate = query.Sum(a => a.RaiseRate);
                        day.RiskCount = query.Sum(a => a.RiskCount);
                        day.Utilization = query.Sum(a => a.Utilization);
                        day.WinRate = query.Sum(a => a.WinRate);
                        day.Yield = query.Sum(a => a.Yield);
                        db.GroupWeekSum.Add(day);
                    }
                }
            }
            db.SaveChanges();
        }

        public GroupWeekSum GetGroupWeekSum(int groupId, int week, int year, DB.DBManager db)
        {
            var query = db.GroupWeekSum.Where(a => a.GroupId == groupId).Where(a => a.Week == week).Where(a => a.Year == year).Include(a => a.Group).ToList();
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }

        public List<GroupWeekSum> GetGroupWeekSum(int week, int year, DB.DBManager db)
        {
            var query = db.GroupWeekSum.Where(a => a.Week == week).Where(a => a.Year == year);
            if (query.Count() > 0)
            {
                return query.ToList();
            }
            else
            {
                return null;
            }
        }
    }
}